
 planet_circle_radius = 10;
 planet_half_border = 1;
 planet_selected_half_border = 2;

 var mode_planet_coordinates = 0;
 var mode_map_image = 2;
 var mode_planet_reflections = 3;
 var mode_planet_stats = 1;
 var mode_starlanes = 4;

 function C_Planet(planet_sid, planet_jsid, planet_name, planet_x, planet_y, planet_size, starlanes_jsids, map_sid)
 {
  this.planet_sid = planet_sid;
  this.planet_jsid = planet_jsid;
  this.planet_name = planet_name;
  this.planet_x = planet_x;
  this.planet_y = planet_y;
  this.planet_size = planet_size;
  this.starlanes_jsids = starlanes_jsids;
  this.map_sid = map_sid;
  this.planet_reflection = CW2_NO_OBJECT;
 }

//array planets - [0] - planet_id(JavaSript one) or CW2_NO_OBJECT, 1 - name, 2 - X, 3 - Y, 4 - reflection's primary planet id or CW2_NO_OBJECT for primary planet, 5 - sql_planet_id, 6 - planet_size, 7 - array(max_starlanes)

function updatePlanetDiv(i, txt, title)
{
 if (i == CW2_NO_OBJECT) {return;}

 div2_id = "planet2_" + i;
 div2 = document.getElementById(div2_id);
 div_id = "planet_" + i;
 div = document.getElementById(div_id);
 
 //alert("updatePlanetDiv: i=" + i + " planet_jsid="+Planets[i].planet_jsid);

 if (Planets[i].planet_jsid == CW2_NO_OBJECT)
 {
  if (div)
  {
   div.style.display = "none";
  }
  if (div2)
  {
   div2.style.display = "none";
  }
  return;
 }
 
 if (!(div2))
 {
  div2 = document.createElement('div');
  div2.id = div2_id;
  div2.style.position = "absolute";
  div2.style.color = "white";
  div2.style.borderColor = "black";
  div2.style.borderStyle = "solid";
  div2.style.fontFamily = "arial";
  div2.style.fontSize = "12pt";
  div2.style.fontWeight = "bold";
  div2.setAttribute('onclick', 'gotClick('+i+', -999, -999)');
  div2.innerHTML = "X";
  map_div.appendChild(div2);
 }
 div2.style.left = Planets[i].planet_x;
 div2.style.top = Planets[i].planet_y;
  
 if (!(div))
 {
  div = document.createElement('div');
  div.id = div_id;
  div.style.position = "absolute";
  
  span = document.createElement('span');
  span.style.color = "white";
  span.style.fontFamily = "arial";
  span.style.fontSize = "9pt";
  span.style.fontWeight = "bold";
  div.appendChild(span);
  map_div.appendChild(div);
 }
 else
 {
  span = div.childNodes[0];
 }

 if (Planets[i].planet_jsid == CW2_NO_OBJECT)
 {
  div.style.display = "none";
 }
 
 if (!txt)
 {
  txt = Planets[i].planet_name;
  if (Planets[i].planet_reflection != CW2_NO_OBJECT)
  {
   txt = "<?php echo jst($lang_cw2_reflection);?>" + txt;
  }
  planet_size = Planets[i].planet_size;
  if ((planet_size < 0) || (planet_size > planet_size_texts.length))
  {
   planet_size = 0;
  }
  txt += "(" + planet_size_texts[planet_size] + ")";
 }
 span.innerHTML = txt;

 if (title)
 {
  div.title = title;
 }

 div.style.left = Planets[i].planet_x;
 div.style.top = Planets[i].planet_y - 24;

 if (i == curr_planet)
 {
  div2.style.color = "red";
  div2.style.borderColor = "white";
  div2.style.fontSize = "16pt";
 }
 else
 {
  div2.style.color = "white";
  div2.style.borderColor = "black";
  div2.style.fontSize = "12pt";
 }
 
 if (Planets[i].planet_jsid != CW2_NO_OBJECT)
 {
  div.style.display = "";
  div2.style.display = "";
  doObjPosition(div);
  doObjPosition(div2);
 }
 else
 {
  div.style.display = "none";
  div2.style.display = "none";
 }
 
 return;
 sl_id = "starlanes_" + i;
 sl = document.getElementById(sl_id);
 if (!sl)
 {
  sl = document.createElement('table');
  sl.id = sl_id;
  sl.style.position = "absolute";
  sl.style.display = "";
  sl.style.color = "white";
  sl.style.fontSize = "8pt";
  for (j in Planets[i].starlanes_jsids)
  {
   tr = document.createElement('tr');
   td = document.createElement('td');
   tr.appendChild(td);
   sl.appendChild(tr);
  }
  map_div.appendChild(sl);
 }
 
 for (j in Planets[i].starlanes_jsids)
 {
  td = sl.childNodes[j].childNodes[0];
  k = Planets[i].starlanes_jsids[j];
  txt = "" + k;
  if ((k != CW2_NO_OBJECT) && (Planets[k].planet_jsid != CW2_NO_OBJECT))
  {
   txt += ":" + Planets[k].planet_name;
  }
  td.innerHTML = txt;
 }
 
 sl.style.left = Planets[i].planet_x + 24;
 sl.style.top = Planets[i].planet_y - 12;
}

function drawStarlane(planet1, planet2, x1, y1, x2, y2)
{
 simple_x = 0;
 simple_y = 0;
 if (Math.abs(x1 - x2) < map_half_x)
 {
  simple_x = 1;
 }
 if (Math.abs(y1 - y2) < map_half_y)
 {
  simple_y = 1;
 }
 if ((simple_x + simple_y) == 2)
 {
  jg.drawLine(x1, y1, x2, y2);
  return;
 }
 
 x = new Array(x1, x2);
 y = new Array(y1, y2);
 for (i = 0; i < 2; i++)
 {
  if (i == 0)
  {
   nx1 = x1;
   nx2 = x2;
   ny1 = y1;
   ny2 = y2;
   src_planet = planet1;
  }
  else
  {
   nx1 = x2;
   nx2 = x1;
   ny1 = y2;
   ny2 = y1;
   src_planet = planet2;
  }
  
  if (simple_x == 0)
  {
   if (nx1 > nx2)
   {
    nx2+= map_x;
   }
   else
   {
    nx2-= map_x;
   }
  }
  if (simple_y == 0)
  {
   if (ny1 > ny2)
   {
    ny2+= map_y;
   }
   else
   {
    ny2-= map_y;
   }
  }
  
  radius_x = 100000;
  radius_y = 100000;
  arc = Math.atan2(nx2 - nx1, ny2 - ny1);
  arcg = Math.round(arc / Math.PI * 180);
  if (arcg > 0)
  {
   dx = map_x - nx1;
  }
  else
  {
   dx = nx1;
  }
  sin_x = Math.sin(arc);
  abs_sin_x = Math.abs(sin_x);
  if (abs_sin_x > 0.001)
  {
   radius_x = dx / abs_sin_x;
  }

  if ((arcg > 90 ) || (arcg < -90))
  {
   dy = ny1;
  }
  else
  {
   dy = map_y - ny1;
  }
  cos_y = Math.cos(arc);
  abs_cos_y = Math.abs(cos_y);
  if (abs_cos_y > 0.001)
  {
   radius_y =  dy / abs_cos_y;
  }
  
  //updatePlanetDiv(src_planet, false, "arcg=" + arcg + "  nx1=" + nx1 + "  dx=" + dx + "  sin_x=" +(Math.round(sin_x*100)/100) + "  radius_x="+Math.round(radius_x) + "  ny1=" + ny1 +"  dy=" + dy + "  cos_y=" +(Math.round(cos_y*100)/100) + "  radius_y="+Math.round(radius_y));
  
  radius = Math.min(radius_x, radius_y);
  nx2 = nx1 + radius * sin_x;
  ny2 = ny1 + radius * cos_y;
  jg.drawLine(nx1, ny1, nx2, ny2);
 }
}

function drawAll()
{
 if ((!Planets) || (!jg)) {return;}
 jg.clear();
 i = curr_planet;

 if (show_all_starlanes == 0)
 {
  if ((i == CW2_NO_OBJECT) || (Planets[i].planet_jsid == CW2_NO_OBJECT)) {return;}
  planet = Planets[i];
  for (j in planet.starlanes_jsids)
  {
   if (planet.starlanes_jsids[j] != CW2_NO_OBJECT)
   {
    planet2 = Planets[planet.starlanes_jsids[j]];
    if (planet2.planet_jsid != CW2_NO_OBJECT)
    {
     jg.setColor("#4040ff");
     jg.setStroke(1);
     drawStarlane(i, planet.starlanes_jsids[j], planet.planet_x, planet.planet_y, planet2.planet_x, planet2.planet_y);
     jg.paint();
    }
   }
  }
 }
 else
 {
  for (i in Planets)
  {
   if (Planets.planet_jsid == CW2_NO_OBJECT) {continue;}
   planet = Planets[i];
   for (j in planet.starlanes_jsids)
   {
    if (planet.starlanes_jsids[j] > i)
    {
     planet2 = Planets[planet.starlanes_jsids[j]];
     if (planet2.planet_jsid != CW2_NO_OBJECT)
     {
      if ((i == curr_planet) || (planet.starlanes_jsids[j] == curr_planet))
      {
       //jg.setColor("#ff4040");
       jg.setColor("#4040ff");
       jg.setStroke(1);
      }
      else
      {
       jg.setColor("#4040ff");
       jg.setStroke(1);
      }
      drawStarlane(i, planet.starlanes_jsids[j], planet.planet_x, planet.planet_y, planet2.planet_x, planet2.planet_y);
     }
    }
   }
  }
 }
 jg.paint();
}

function updateImageSize()
{
 map_img = document.getElementById("map_img");
 
 map_x = map_img.offsetWidth;
 map_half_x = Math.round(map_x / 2);
 
 map_y = map_img.offsetHeight;
 map_half_y = Math.round(map_y / 2);
}

function doOnLoad()
{
 updateImageSize();
 drawAll();
}

function selectTR(tr, show_tr)
{
 elem = document.getElementById(tr);
 if (tr == show_tr)
 {
  elem.style.display = "";
 }
 else
 {
  elem.style.display = "none";
 }
}

function selectTRs(show_tr)
{
 selectTR('tr_planet_stats', show_tr);
 selectTR('tr_map_image', show_tr);
 selectTR('tr_nothing', show_tr);
 selectTR('tr_starlanes', show_tr);
}

function fillPlanetStats()
{
 if ((curr_planet == CW2_NO_OBJECT) || (Planets[curr_planet].planet_jsid == CW2_NO_OBJECT))
 {
  planet_name = '';
  planet_size = '';
 }
 else
 {
  planet_name = Planets[curr_planet].planet_name;
  planet_size = Planets[curr_planet].planet_size;
 }
 document.getElementById("planet_name").value = planet_name;
 document.getElementById("planet_size").value = planet_size;
 
 select = document.getElementById("planet_map");
 select.selectedIndex = 0;
 len = select.length;
 map_sid = Planets[curr_planet].map_sid;
 for (i = 0; i < len; i++)
 {
  if (select[i].value == map_sid)
  {
   select.selectedIndex = i;
   break;
  }
 }
}

function changePlanetName()
{
 if ((curr_planet == CW2_NO_OBJECT) || (Planets[curr_planet].planet_jsid == CW2_NO_OBJECT))
 {
  return;
 }
 Planets[curr_planet].planet_name = document.getElementById("planet_name").value;
 fillPlanetStats();
 updatePlanetDiv(curr_planet);
}

function changePlanetMap()
{
 if ((curr_planet == CW2_NO_OBJECT) || (Planets[curr_planet].planet_jsid == CW2_NO_OBJECT))
 select = document.getElementById("planet_map");
 i = select.selectedIndex;
 Planets[curr_planet].map_sid = select.options[i].value;
}

function changePlanetSize()
{
 if ((curr_planet == CW2_NO_OBJECT) || (Planets[curr_planet].planet_jsid == CW2_NO_OBJECT))
 {
  return;
 }
 val = parseInt(document.getElementById("planet_size").value);
 if ((val < 0) || (val > planet_size_texts.length))
 {
  val = 0;
 }
 Planets[curr_planet].planet_size = val;
 fillPlanetStats();
 updatePlanetDiv(curr_planet);
}

function changeEditMode(mode)
{
 edit_mode = mode;
 selectTRs('tr_nothing');
 elem = document.getElementById("edit_mode_text");
 if (edit_mode == mode_planet_coordinates)
 {
  elem.innerHTML = lang_cw2_editing_planet_coordinates;
 }
 else if (edit_mode == mode_planet_stats)
 {
  elem.innerHTML = lang_cw2_editing_planet_information;
  document.getElementById("planet_name").value = "";
  selectTRs('tr_planet_stats');
 }
 else if (edit_mode == mode_map_image)
 {
  elem.innerHTML = lang_cw2_editing_map_image;
  selectTRs('tr_map_image');
 }
 else if (edit_mode == mode_planet_reflections)
 {
  elem.innerHTML = lang_cw2_editing_planet_reflections;
 }
 else if (edit_mode == mode_starlanes)
 {
  elem.innerHTML = lang_cw2_editing_starlanes;
  selectTRs('tr_starlanes');
 }
 else if (edit_mode == -1)
 {
  elem.innerHTML = lang_cw2_tournament_not_selected;
 }
 else
 {
  elem.innerHTML = "unknown edit mode? '" + mode + "'";
 }
 drawAll();
}

function findPlanetByXY(x, y)
{
 for (i in Planets)
 {
  if (Planets[i].planet_jsid == CW2_NO_OBJECT)
  {
   continue;
  }
  dX = Planets[i].planet_x - x;
  dY = Planets[i].planet_y - y;
  if ((dX * dX + dY * dY) < (planet_circle_radius * planet_circle_radius))
  {
   return i;
  }
 }
 return CW2_NO_OBJECT;  
}

function removeStarlane(p1, p2)
{
 if ((p1 == CW2_NO_OBJECT) || (Planets[p1].planet_jsid == CW2_NO_OBJECT) || (p2 == CW2_NO_OBJECT) || (Planets[p2].planet_jsid == CW2_NO_OBJECT)) {return false;}
 if (Planets[p2].planet_reflection != CW2_NO_OBJECT) {p2 = Planets[p2].planet_reflection};
 found = 0;
 for (i in Planets[p1].starlanes_jsids)
 {
  if (Planets[p1].starlanes_jsids[i] == p2)
  {
   Planets[p1].starlanes_jsids[i] = CW2_NO_OBJECT;
   found = found + 1;
   break;
  }
 }
 for (i in Planets[p2].starlanes_jsids)
 {
  if (Planets[p2].starlanes_jsids[i] == p1)
  {
   Planets[p2].starlanes_jsids[i] = CW2_NO_OBJECT;
   found = found + 1;
   break;
  }
 }
 return (found == 2);
}

function addStarlane(p1, p2)
{
 if ((p1 == CW2_NO_OBJECT) || (Planets[p1].planet_jsid == CW2_NO_OBJECT) || (p2 == CW2_NO_OBJECT) || (Planets[p2].planet_jsid == CW2_NO_OBJECT)) {return false;}
 if (Planets[p2].planet_reflection != CW2_NO_OBJECT) {p2 = Planets[p2].planet_reflection};
 a = new Array(p1, p2, -1, -1);
 hasit = 0;
 for (i=0; i<2; i++)
 {
  planet = Planets[a[i]];
  starlanes = Planets[a[i]].starlanes_jsids;
  for (j in starlanes)
  {
   if ((starlanes[j] == CW2_NO_OBJECT) || (Planets[j].planet_jsid == CW2_NO_OBJECT))
   {
    a[i+2] = j;
   }
   else if (starlanes[j] == a[1-i])
   {
    hasit = 1;
   }
  }
 }
 if ((hasit == 0) && (a[2] > -1) && (a[3] > -1))
 {
  Planets[p1].starlanes_jsids[a[2]] = p2;
  Planets[p2].starlanes_jsids[a[3]] = p1;
 }
}

function gotClick(pl, x, y)
{
 old_curr_planet = curr_planet;
 //alert("Click: planet=" + pl + "  x=" + x + "  y=" + y);
 if (edit_mode == mode_planet_coordinates) //*********
 {
  i = pl;
  if (i != CW2_NO_OBJECT)
  {
   Planets[i].planet_jsid = CW2_NO_OBJECT;
   for (j in Planets)
   {
    if (Planets[j].planet_reflection == i)
    {
     Planets[j].planet_jsid = CW2_NO_OBJECT; //clear all reflections too
    }
   }
   updatePlanetDiv(i);
   drawAll();
   return;
  }

  last_i = 0;
  found = 0;
  for (i in Planets)
  {
   last_i = i;
   if (Planets[i].planet_jsid == CW2_NO_OBJECT)
   {
    found = 1;
    break;
   }
  }
  if (found == 0)
  {
   last_i++;
  }
  Planets[last_i] = new C_Planet(CW2_NO_SID, last_i, '???', x, y, 0, new Array(), 0);
  for (j = 0; j < max_starlanes; j++)
  {
   Planets[last_i].starlanes_jsids[j] = CW2_NO_OBJECT;
  }
  updatePlanetDiv(last_i);
  drawAll();
 } 
 else if (edit_mode == mode_planet_stats) //*********
 {
  curr_planet = pl;
  if ((curr_planet != CW2_NO_OBJECT) && (Planets[curr_planet].planet_reflection != CW2_NO_OBJECT))
  {
   curr_planet = Planets[curr_planet].planet_reflection;
  }
  updatePlanetDiv(old_curr_planet);
  updatePlanetDiv(curr_planet);
  fillPlanetStats();
  drawAll();
 }
 else if (edit_mode == mode_planet_reflections) //*********
 {
  i = pl;
  if (i == CW2_NO_OBJECT)
  {
   return;
  }
  if (curr_planet == CW2_NO_OBJECT)  //select main
  {
   if (Planets[i].planet_reflection != CW2_NO_OBJECT)
   {
    i = Planets[i].planet_reflection; //selecting main instead of refllection
   }
   curr_planet = i;
  }
  else if (i == curr_planet)
  {
   curr_planet = CW2_NO_OBJECT; //unselect main
  }
  else //maybe reflections
  {
   if (Planets[i].planet_reflection == CW2_NO_OBJECT)
   {
    Planets[i].planet_reflection = curr_planet; //becomes reflection of main
   }
   else if (Planets[i].planet_reflection == curr_planet)
   {
    Planets[i][4] = CW2_NO_OBJECT; //no longer a reflection
   }
  }
  drawAll();
 }
 else if (edit_mode == mode_starlanes) //*********
 {
  click = pl;
  if (click == CW2_NO_OBJECT)
  {
   return;
  }
  if (curr_planet == CW2_NO_OBJECT)
  {
   curr_planet = click;
  }
  else if (curr_planet == click)
  {
   curr_planet = CW2_NO_OBJECT;
  }
  else
  {
   if (!removeStarlane(click, curr_planet))
   {
    addStarlane(click, curr_planet);
   }
   updatePlanetDiv(click);
   updatePlanetDiv(curr_planet);
  }
  updatePlanetDiv(old_curr_planet);
  updatePlanetDiv(curr_planet);
  drawAll();
 }

}

function gotClickXY(e)
{
 elem = document.getElementById("map_div");
 txt = document.getElementById("edit_mode_text");
 x = e.pageX - elem.offsetLeft;
 y = e.pageY - elem.offsetTop;
 pl = findPlanetByXY(x, y);
 gotClick(pl, x, y);
}

function changeMapImageURL()
{
 new_map_img_url = document.getElementById("map_img_url").value;
 document.getElementById("map_img").src = new_map_img_url;
 updateImageSize();
}

function doSave()
{
 form = document.getElementById("form_id");
 addInput(form, "tournament_id", tournament_id);
 addInput(form, "edit_mode", edit_mode);
 new_map_img_url = document.getElementById("map_img_url").value;
 if (new_map_img_url)
 {
  addInput(form, "map_img_url", new_map_img_url);
 }
 
 npl = 0;
 for (i in Planets)
 {
  if (Planets[i].planet_jsid == CW2_NO_OBJECT) {continue;}
  npl++;
  planet = Planets[i];
  addInput(form, "planet_name_"+npl, planet.planet_name);
  planet_data = "" + planet.planet_x + "_" + planet.planet_y + "_" + planet.planet_sid + "_" + planet.planet_size + "_" + planet.map_sid;
  for (j in planet.starlanes_jsids)
  {
   starlane_jsid = planet.starlanes_jsids[j];
   starlane_sid = CW2_NO_OBJECT;
   if (starlane_jsid >= 0)
   {
    starlane_sid = Planets[starlane_jsid].planet_sid;
   }
   planet_data += "_" + starlane_sid;
  }
  addInput(form, "planet_data_"+npl, planet_data);
 }
}

function changeShowAllStarlanes()
{
 input = document.getElementById("show_all_starlanes");
 show_all_starlanes = 0;
 if (input.checked)
 {
  show_all_starlanes = 1;
 }
 drawAll();
}

function deleteAllStarlanes()
{
 for (i in Planets)
 {
  changed = false;
  for (j in Planets[i].starlanes_jsids)
  {
   if (Planets[i].starlanes_jsids[j] != CW2_NO_OBJECT)
   {
    Planets[i].starlanes_jsids[j] = CW2_NO_OBJECT;
    changed = true;
   }
  }
  if (changed)
  {
   updatePlanetDiv(i);
  }
 }
 drawAll();
}
